let bus = {
    eventMap: {}
};

bus.on = function(name, fun) {
    if (name && fun) {
        if (this[name]) {
            this.eventMap[name].indexOf(fun) < 0 && this.eventMap[name].push(fun);
        } else {
            let execute = function(a, b, c, e, f, g, h, j, k, l) {
                let list = execute.root.eventMap[execute.key];
                let rs = [];
                for (let i = 0; i < list.length; i ++) {
                    rs.push(list[i](a, b, c, e, f, g, h, j, k, l));
                }
                return rs;
            };
            execute.key = name;
            execute.root = this;
            this[name] = execute;
            this.eventMap[name] = [fun];
        }
    }
};

bus.off = function(name, fun) {
    if (name && fun) {
        let list = this.eventMap[name];
        if (list) {
            for (let i = list.length - 1; i >= 0; i --) {
                if (list[i] == fun) {
                    list.splice(i, 1);
                }
            }
            if (list.length == 0) {
                delete this[name];
                delete this.eventMap[name];
            }
        }
    }
};

export default bus;